﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhatPhucLoc.model;
using PhatPhucLoc.dao;

namespace PhatPhucLoc.view
{
    public partial class FormThemNhomHang : Form
    {
        private FormNhomHang formNhomHang = null;
        private NhomHangModel nhomHangModel;
        public FormThemNhomHang()
        {
            InitializeComponent();
        }

        public FormThemNhomHang(NhomHangModel _nhomHangModel, ref FormNhomHang _formNhomHang)
        {
            InitializeComponent();
            FormChiTietKhachHangLoad();
            formNhomHang = _formNhomHang;

            // load information khach mua hang
            nhomHangModel = _nhomHangModel;
            txtTen.Text = nhomHangModel.Ten;
            txtGhiChu.Text = nhomHangModel.GhiChu;
        }


        public FormThemNhomHang(ref FormNhomHang _formNhomHang)
        {
            InitializeComponent();
            FormChiTietKhachHangLoad();
            formNhomHang = _formNhomHang;
        }

        public void FormChiTietKhachHangLoad()
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietKhachHang_KeyDown);
        }

        private void FormChiTietKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtTen.Text.Equals(""))
            {
                MessageBox.Show("Tên Nhóm Hàng Không Được Rỗng");
                txtTen.Focus();
            }
            else
            {
                if (nhomHangModel == null)
                {
                    nhomHangModel = new NhomHangModel();

                }

                nhomHangModel.Ten = txtTen.Text;
                nhomHangModel.GhiChu = txtGhiChu.Text;
                // new
                if (nhomHangModel.Id == null || nhomHangModel.Id == "")
                {
                    if (NhomHangDAO.Insert(nhomHangModel))
          
                        MessageBox.Show("Thêm Nhóm hàng thành công");
                        if (formNhomHang != null)
                        {
                            formNhomHang.formNhomHangLoad();
                        }
                        this.Dispose();
             
                }
                
                else
                {
                    if (NhomHangDAO.Update(nhomHangModel))
                    {
                        MessageBox.Show("Cập nhật Nhóm hàng thành công");
                        formNhomHang.formNhomHangLoad();
                        this.Dispose();
                    }
                }
            }
        }

    }
}
